package com.ctrip.ec.biz.dao.cart;
import com.ctrip.ec.biz.entity.order.OrderStatus;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.cart.CartEntity;
import com.ctrip.ec.dal.DbAccess;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class CartDao extends DaoBase<CartEntity> {
    public CartDao() {
        super(CartEntity.class);
    }
    public CartDao(DbAccess db) {
        super(CartEntity.class,db);
    }

    public boolean existSpecId(Long specId, Long memberId, Long id) throws SQLException {
        String sql = "select count(*) from " + this.getTableName() + " where spec_id=@0 and member_id=@1 ";
        if(id !=null){
            sql += " and id != " + id;
        }
        Long count  = this.db.getScalar(Long.class,sql,specId,memberId);
        return count>0;
    }

    public CartEntity entityBySpecId(Long specId, Long memberId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where spec_id=? and member_id=?";
        List<CartEntity> list = this.db.query(CartEntity.class, sql,specId,memberId);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    public void expire() throws SQLException {
        String sql = "delete from " + this.getTableName() +  " where date_add(create_time, interval 15 minute) < now() ";
        this.db.execute(sql);
    }
}
